<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.7.2/axios.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/js-cookie/3.0.5/js.cookie.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>
    <script src="../../js/http.js"></script>
    <title>商品列表</title>
    <link rel="stylesheet" href="../../css/index.css">
    <link href="./css/list.css" rel="stylesheet">

<body>
<div id="root">
    <ul id="commodityList" class="commodityList">
        <li class="commodity">
            <ul>
                <li>图片</li>
                <li class="title">商品名</li>
                <li class="price">商品原价格</li>
                <li class="current_price">商品现价</li>
                <li class="goods_number">商品数量</li>
                <li class="category">商品种类</li>
                <li class="operate">操作</li>
            </ul>
        </li>
        <li id="commodity" class="commodity" v-for="commodity in commodityList" :key="commodity.id">
            <ul>
                <li><img :src="commodity.img_big_logo" alt=""></li>
                <li class="title"><h5>{{commodity.title}}</h5></li>
                <li class="price"><p><span>商品原价格：</span>{{'￥' + commodity.price}}</p></li>
                <li class="current_price"><p><span>商品现价：</span>{{'￥' + commodity.current_price}}</p></li>
                <li class="goods_number"><p>{{commodity.goods_number}}</p></li>
                <li class="category"><p>{{commodity.category}}</p></li>
                <li class="operate">
                    <button @click="del_com(commodity.goods_id)">删除</button>
                    <button @click="updata_com(commodity.goods_id)">修改</button>
                </li>
            </ul>
        </li>

        <li id="add_commodity" class="add_commodity">
            <a href="./add.html">
                添加
            </a>
        </li>
    </ul>

</div>
<script>

  new Vue({
    el: '#root',
    data() {
      return {
        id: (() => {
          const path = new URLSearchParams(location.search);
          return path.get('id');
        })(),
        commodityList: [],
      };
    },
    methods: {
      del_com(goods_id) {
        console.log(goods_id);
        ajax({
          url: '/admin/goods/' + goods_id,
          method: 'DELETE',
          headers: {
            'Authorization': Cookies.get('token'),
          },
        }).then((r) => {
          if (r.data.code === 1) {
            this.get_commodityList();
          } else {
            alert(r.data);
          }
        });
      },
      updata_com(goods_id) {
        location.href = 'updata.html?goods_id=' + goods_id;
      },
      get_commodityList() {
        ajax({
          url: '/admin/goods',
          method: 'GET',
          headers: {
            'Authorization': Cookies.get('token'),
          },
        }).then((r) => {
          if (r.data.code === 1) {
            this.commodityList = r.data.list;
            console.log(this.commodityList);
          } else {
            alert('数据加载失败！'); //
            location.href = '../../login.html';
          }
        });
      },

    },
    mounted() {
      const that = this;
      that.get_commodityList();

    },
  });
</script>

</body>

</html>